import { useUserStore } from '@/store/modules/useUserStore';
import request from '@/utils/common/request/index';
// #ifdef H5
import { H5Login } from '@/utils/h5/index';
// #endif
import { useGetThemColor } from '@/hooks/useGetThemColor';

const { primaryColor } = useGetThemColor();
/**
 *  @description: 处理重新登录
 *  @return void
 */
export function handleReLogin() {
	// * 清空用户缓存数据和storage
	const userStore = useUserStore();
	userStore.claerData();
	uni.removeStorageSync('token');

	// * H5提示授权登录框 
	// #ifdef H5
	uni.showModal({
		title: '提示',
		content: '登录失效！请您重新登录',
		showCancel: false,
		confirmColor: primaryColor.value,
		success: function(res) {
			// res.confirm && request({
			// 	url: '/api/gettesttoken',
			// 	method: 'POST',
			// 	data: {
			// 		user_id: '48339167'
			// 	}
			// }).then(res => {
			// 	uni.setStorageSync('token', res.token)
			// });
			res.confirm && H5Login();
		}
	})
	// #endif

	// *  微信小程序直接获取code登录，不提示
	// #ifdef MP-WEIXIN
	uni.stopPullDownRefresh();
	wx.login({
		//获取code
		withCredentials: true,
		success(res) {
			let reqData = { code: res.code };
			request({
				url: '/wechat/login',
				method: 'POST',
				loadingTip: '授权中',
				data: reqData,
			}).then(res => {

			})
		},
	});
	// #endif
}